package com.chiaching.chaoxingsecondkill;

import com.chiaching.chaoxingsecondkill.config.UserInfoConfig;
import com.chiaching.chaoxingsecondkill.hander.SecondKillHandler;
import com.chiaching.chaoxingsecondkill.service.IMailService;
import com.chiaching.chaoxingsecondkill.utils.DingUtil;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Objects;

/**
 *  黄波 http://120.77.156.103:8081/user/config/setCookies?phone=18973839925&cookies=
 *  同学 http://120.77.156.103:8081/user/config/setCookies?phone=18890025513&cookies=
 *  手动开始抢座位 http://120.77.156.103:8081/user/config/start
 *
 *  app页面 index：http://office.chaoxing.com/front/third/apps/seat/index?appId=1000&fidEnc=cc061368ad68d8d1&uid=83751214&mappId=4950307&code=A9AOHmlv&state=33045
 *
 *  信息推送工具 https://pushbear.ftqq.com/admin/#/channel
 */
@SpringBootApplication
@ComponentScan("com.chiaching")
@EnableScheduling
@Slf4j
@RefreshScope
@EnableDiscoveryClient
@MapperScan(basePackages = "com.chiaching.chaoxingsecondkill.mapper")
public class ChaoxingSecondkillApplication {

    public static void main(String[] args) {
        SpringApplication.run(ChaoxingSecondkillApplication.class, args);
    }


    @Resource
    SecondKillHandler secondKillHandler;
    @Resource
    UserInfoConfig UserConfig;


    //@PostConstruct
    @Scheduled(cron = "0 0/1 * * * ? ")
    public void start() {
        secondKillHandler.main(5, SecondKillHandler.TIME_INTERVAL_TWO,Boolean.FALSE);
    }

    @Scheduled(cron = "57 59 19 * * ?")
    public void scheduled() {
        log.info("----定时任务开始执行------");
        SecondKillHandler.START.set(Boolean.TRUE);
        secondKillHandler.main(30,SecondKillHandler.TIME_INTERVAL_ONE,Boolean.TRUE);
    }

    @Scheduled(cron = "00 59 19 * * ?")
    public void test() {
        log.info("----定时任务预计还有57秒开始执行------");
    }


    /**
     * 每天凌晨清除已经抢座成功的信息
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void clear() {
        UserConfig.getList().stream().filter(Objects::nonNull).forEach(e -> e.getTimes().clear());
        log.info("----清除抢座信息完毕!------");
    }


    @Scheduled(cron = "0 3 0/1 * * ?")
    public void clearHash() {
        log.info("校验重复hash [{}]",DingUtil.getHashSet());
        DingUtil.getHashSet().clear();
        log.info("----清除校验重复hash缓存完毕!------");
    }


    @Scheduled(cron = "0 30 20 * * ?")
    //@PostConstruct
    public void notifyMsg() {
        secondKillHandler.emaillHandler();
        log.info("--------邮件发送处理完成-------");
    }

}
